package cn.blogger.controller;

import cn.blogger.entity.Result;
import cn.blogger.pojo.TBlog;
import cn.blogger.pojo.TComment;
import cn.blogger.pojo.TUser;
import cn.blogger.service.CommenService;
import cn.blogger.vo.CommentVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {
    @Reference
    private CommenService commenService;
    @PostMapping(value="/add.do")
    public Result add(@RequestParam(value="userId")Integer userId,
                      @RequestParam(value="type",defaultValue = "0")Boolean type,
                      @RequestParam(value="content")String content,
                      @RequestParam(value="source")String source,
                      @RequestParam(value="blog_uid")@Nullable Integer blog_uid,
                      @RequestParam(value="to_uid")@Nullable Integer to_uid,
                      @RequestParam(value="to_user_uid")@Nullable Integer to_user_uid){
        System.out.println("进入请求");
        TComment comment = new TComment(userId,to_uid,to_user_uid,blog_uid,type,content,source);
        try {
            commenService.addComment(comment);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"评论添加失败");
        }
        return new Result(true,"评论添加成功");
    }
    //分页查询所有的评论
    @GetMapping(value="selectAll.do")
    public Result selectAll(@RequestParam(value="currentPage")Integer currentPage,
                        @RequestParam(value="pageSize")Integer pageSize){
        List<TComment> list;
        try {
            list =commenService.listAll(currentPage, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"评论加载失败");
        }
        return new Result(true,"评论加载成功",list);
    }
//  分页查询博客或留言板下的全部评论
    @GetMapping(value="query.do")
    public Result query(@RequestParam(value="currentPage")Integer currentPage,
                        @RequestParam(value="pageSize")Integer pageSize,
                        @RequestParam(value="source")String source,
                        @RequestParam(value="blog_uid")@Nullable Integer blog_uid){

        CommentVO commentVO = new CommentVO(source,blog_uid);
        commentVO.setPageSize(pageSize);
        commentVO.setCurrentPage(currentPage);
//        System.out.println("进入请求");
        List<TComment> list;
        try {
            list =commenService.queryCommentByCondition(commentVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"评论加载失败");
        }
        return new Result(true,"评论加载成功",list);
    }
    //根据uid删除评论及其所有下级评论
    @DeleteMapping(value="delete.do")
    public Result deleteComment(@RequestParam(value="uid")Integer uid){
        System.out.println("进入请求"+ uid);
        try {
            commenService.deleteComment(uid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"评论删除失败");
        }
        return new Result(true,"评论删除成功");
    }

    //测试删除某用户的所有评论
    @RequestMapping("deleteUser.do")
    public Result deleteCommentOwnedByUser(@RequestBody TUser user){
        System.out.println("进入请求"+ user.getUid());
        try {
            commenService.userCommentDelete(user.getUid());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户评论删除失败");
        }
        return new Result(true,"用户评论删除成功");
    }
    //测试删除某blog的所有评论
    @RequestMapping("deleteBlog.do")
    public Result deleteCommentOwnedByBlog(@RequestBody TBlog blog){
        System.out.println("进入请求"+ blog.getBid());
        try {
            commenService.blogCommentDelete(blog.getBid());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户评论删除失败");
        }
        return new Result(true,"用户评论删除成功");
    }

}
